/*
============================================================================
 Name        : PdTimer.cpp
 Author      : Jian(raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : CPdTimer implementation
============================================================================
*/

#include "PdTimer.h"

#define __MAX_INT   (0x7FFFFFFF)

CPdTimer::CPdTimer(pd_timer_type aType) : 
CTimer(CActive::EPriorityStandard),
iType(aType)
{

}

CPdTimer::~CPdTimer()
{
    Cancel() ;
}

CPdTimer* CPdTimer::NewL(pd_timer_type aType)
{
    CPdTimer* self = new (ELeave)CPdTimer(aType);
    CleanupStack::PushL(self);
    self->_initL();
	CleanupStack::Pop(); 
	return self;
}

void CPdTimer::_initL()
{
    CTimer::ConstructL();
    CActiveScheduler::Add(this);
}

void CPdTimer::Start(DD_UINT time_millisecond, pd_timer_callback callback, void* ptr) 
{
    if (IsActive()) Cancel() ;
    
    iCallBack = callback ;
    iSavePtr  = ptr ;

    iTimerTime = time_millisecond * 1000 ;
    
    if (iTimerTime < 0)  {
        DD_LOG("CPdTimer::Start is time = %d is too big, fix it to the legal data", iTimerTime) ;
        iTimerTime = __MAX_INT ;
    }
    
    After(iTimerTime) ;
}

void CPdTimer::Stop() 
{
    if (IsActive()) Cancel() ;
}

void CPdTimer::RunL()
{
    if (iStatus.Int() != KErrNone) 
        DD_LOG("CPdTimer::RunL is error = %d", iStatus.Int()) ;

    iCallBack(iSavePtr) ;

    if (iType == PD_REPEAT) 
        After(iTimerTime) ;
}

TInt CPdTimer::RunError(TInt aError) 
{
    if (iStatus.Int() != KErrNone) 
        DD_LOG("CPdTimer::RunError is call and error = %d", iStatus.Int()) ;

    iCallBack(iSavePtr) ;

    if (iType == PD_REPEAT) 
        After(iTimerTime) ;
}

/* End of File */
